package com.ruoyi.web.controller.system;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;

/**
 * 上传相关的Controller
 * 
 * @author ruoyi
 * @date 2020-06-10
 */
@Controller
@RequestMapping("/system/upload")
public class AdminUploadController extends BaseController
{
    //上传真实地址
//	private  String uploadFolder=Global.getUploadFolder();
	
    //映射地址
//	private static String uploadUrl=Global.getUploadUrl();
	
	//端口号
//	private static String wpost=Global.getWpost();
	
    /**
     * 上传图片
     * @throws Exception 
     */
    @RequiresPermissions("system:upload:pictrue")
    @Log(title = "上传图片", businessType = BusinessType.IMPORT)
    @PostMapping("/pictrue")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception
    {
    	String ip = IpUtils.getIpAddr(request);
	    String qurl="http://"+ip+":"+Global.getWpost()+Global.getUploadUrl();
	    
	    File filepath = new File(Global.getUploadFolder());
	    if (!filepath.exists()){
	    	filepath.mkdirs();
        }
        String filezurl = FileUploadUtils.uploadFile(Global.getUploadFolder(), file);
        String pictrueUrl=qurl+filezurl;
        return pictrueUrl;
    }
    
    /**
     * 上传图片
     * @throws Exception 
     */
    @RequiresPermissions("system:upload:music")
    @Log(title = "上传音频", businessType = BusinessType.IMPORT)
    @PostMapping("/music")
    @ResponseBody
    public Map<String,Object> music(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception
    {
    	String ip = IpUtils.getIpAddr(request);
	    String qurl="http://"+ip+":"+Global.getWpost()+Global.getUploadUrl();
	    
	    File filepath = new File(Global.getUploadFolder());
	    if (!filepath.exists()){
	    	filepath.mkdirs();
        }
        Map<String,Object> resMap= FileUploadUtils.uploadMusicFile(Global.getUploadFolder(), file);
        resMap.put("fileName", qurl+resMap.get("fileName"));
        return resMap;
    }
    
}
